//
//  ViewController.m
//  14-HomeWork-礼物说
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "CategoryCollectionReusableView.h"
#import "ColumnCollectionReusableView.h"
#define kWidth CGRectGetWidth(self.view.bounds)
#define kHeight CGRectGetHeight(self.view.bounds)
#define kCollectionViewCellReuse @"reuse"
#define kCollectionViewCategoryHeaderReuse @"categoryHeader"
#define kCollectionViewColumnHeaderReuse @"columnHeader"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *categoryCollection;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createCollection];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"单品"]];
    segment.frame = CGRectMake(0, 0, 150, 32);
    self.navigationItem.titleView = segment;
    self.view.backgroundColor = [UIColor colorWithWhite:0.743 alpha:1.000];
}
#pragma mark - 创建collection
- (void)createCollection {
    // 创建布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 创建collection
    self.categoryCollection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:_categoryCollection];
    // 签订代理人
    _categoryCollection.dataSource = self;
    _categoryCollection.delegate = self;
    // 设置item的大小及边距
    layout.itemSize = CGSizeMake((kWidth - 30) / 2, (kWidth - 30) * 3.3 / 10);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    // 注册
    [_categoryCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
     [_categoryCollection registerClass:[CategoryCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewCategoryHeaderReuse];
    _categoryCollection.backgroundColor = [UIColor whiteColor];
    
    [_categoryCollection registerClass:[ColumnCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewColumnHeaderReuse];
}
#pragma mark - datasource 协议
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
    return 20;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ColumnCollectionReusableView *columnReuseable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewColumnHeaderReuse forIndexPath:indexPath];
//        columnReuseable.backgroundColor = [UIColor colorWithRed:1.000 green:0.513 blue:0.473 alpha:1.000];
        return columnReuseable;
    } else {
        CategoryCollectionReusableView *reuseable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewCategoryHeaderReuse forIndexPath:indexPath];
//        reuseable.backgroundColor = [UIColor cyanColor];
        return reuseable;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, kHeight * 2.6/ 4);
    } else {
        return CGSizeMake(0, 50);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
